package scantask.containers;

import scantask.client.threeDObjects.LeafPolygone;
import scantask.client.threeDObjects.PolygonePool;
import scantask.client.threeDObjects.exceptions.PolygoneCreationException;
import scantask.containers.exceptions.IllegalLocationNameExeption;
import scantask.containers.exceptions.InvalidMeasurementTypeException;
import scantask.containers.exceptions.UnInitializeObjectException;

public class Pest extends Measurement{
	
	private String _type;
	
	private boolean _interpolate;
	
	private int _radius;
	
	public Pest(Coordinate coordinate, ZoomLevel viewInterval, double value, WeekDate date, boolean interpolated, String type, String areaName) throws UnInitializeObjectException, InvalidMeasurementTypeException, IllegalLocationNameExeption {
		super(coordinate, areaName, viewInterval, value, date);
		
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new Pest illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new Pest illegal type");
			throw new InvalidMeasurementTypeException();
		}
		
		this._type = type;
		
		this._interpolate = interpolated;
	}
	
	public Pest(Coordinate coordinate, ZoomLevel viewInterval, PolygonePool pool,  double value, WeekDate date, boolean interpolated, String type, String areaName) throws UnInitializeObjectException, InvalidMeasurementTypeException {
		super(coordinate, areaName, viewInterval, pool, value, date);
		
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new Pest illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed construct new Pest illegal type");
			throw new InvalidMeasurementTypeException();
		}
		
		this._type = type;
		
		this._interpolate = interpolated;
	}


	@Override
	public String getType() {
		if (this.isInterpolated()) {
			return "Measurement" + "\t" + "Pest" + "\t" + this._type + "\t" + "Interpolated";
		}
		return "Measurement" + "\t" + "Pest" + "\t" + this._type + "\t" + "NotInterpolated";
	}
	
	@Override
	public String getCleanType() {
		return this._type;
	}
	
	public boolean isInterpolated() {
		return this._interpolate;
	}
	
	public int getRadius() {
		return this._radius;
	}
	
	public void setType(String type) throws InvalidMeasurementTypeException {
		if (type.compareTo("") == 0) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed set Pest illegal type");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed set Pest illegal type");
			throw new InvalidMeasurementTypeException();
		}
		
		this._type = type;
	}
	
	public void setInterpolated(boolean interpolate) {
		this._interpolate = interpolate;
	}
	
	public void setRadius(int radius) {
		this._radius = radius;
	}

	@Override
	public void buildDisplayedObject(PolygonePool polygonePool) throws PolygoneCreationException {
		this._polygone = polygonePool.getPolygone(this.getType(), this.getRadius());
		this.setDefaultPolygoneAccordingly();
	}
	
	private void setDefaultPolygoneAccordingly() {
		this._polygone.setLocation(this.getCoordinate());
		/** Measurement object MUST be LeafPolygone **/
		((LeafPolygone)this._polygone).addValueToUp(scantask.containers.PestsLevelsSigelton.getPestValueOfLevel(scantask.containers.PestsLevelsSigelton.getPestLevel(this.getValue())));
		((LeafPolygone)this._polygone).setValue(this.getValue());
	}

	@Override
	public String getToolTipDescription() {
		if (this.isInterpolated()) {
			return "Interpolated Mesurement (Pest - " + this.getCleanType() + ")\n" + PestsLevelsSigelton.getPestLevel(this.getValue()) + " (" + this.getValue() + ")";
		}
		return "Mesurement (Pest - " + this.getCleanType() + ")\n" + PestsLevelsSigelton.getPestLevel(this.getValue()) + " (" + this.getValue() + ")";
	}
	
}
